/**
 * Created by IntelliJ IDEA.
 * User: abdelkarim
 * Date: 16-Apr-2008
 * Time: 1:40:23 PM
 * To change this template use File | Settings | File Templates.
 */
class CommentController {
    AuthenticateService authenticateService
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ commentList: Comment.list( params ) ]
    }

    def show = {
        def comment = Comment.get( params.id )

        if(!comment) {
            flash.message = "Comment not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ comment : comment ] }
    }

    def delete = {
        def comment = Comment.get( params.id )
        if(comment) {
            comment.status = Comment.STATUS_DELETED
            flash.message = "Comment ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Comment not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def comment = Comment.get( params.id )

        if(!comment) {
            flash.message = "Comment not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ comment : comment ]
        }
    }

    def update = {
        def comment = Comment.get( params.id )
        if(comment) {
            comment.properties = params
            if(!comment.hasErrors() && comment.save()) {
                flash.message = "Comment ${params.id} updated"
                redirect(action:show,id:comment.id)
            }
            else {
                render(view:'edit',model:[comment:comment])
            }
        }
        else {
            flash.message = "Comment not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def comment = new Comment()
        comment.properties = params
        return ['comment':comment]
    }

    def save = {
        def tttt = params
        def comment = new Comment(params)
        def user =   Person.findWhere(username:authenticateService?.principal()?.username)
        def blog =  Content.get(params.contentID)

        if(!user || !blog){
            render(controller:"comment",view:'create',model:[comment:comment])
        }
//        if(!user?.profile){
//            //create profile
//            def prof = new MemberProfile()
//            prof.save(flush:true)
//            user.profile = prof
//        }

        comment?.createdBy =   user
        comment?.blog    = blog
        if(!comment.hasErrors() && comment.save()) {
            if(blog){
                blog.addToComments(comment)
            }
            flash.message = "Comment ${comment.id} created"
            redirect(controller:"content",action:show,id:blog.id)
        }
        else {
            render(controller:"comment",view:'create',model:[comment:comment])
        }
    }

}